package com.example.demo.controller;

import com.example.demo.interceptor.Constants;
import com.example.demo.model.UserInfo;
import com.example.demo.servicer.Userservice;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.events.Event;

@Slf4j
@ResponseBody
@RestController
@RequestMapping("/use")
public class UseController {
    @Autowired
   private Userservice userservice;
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpSession session){

        log.info(userName);
        log.info(password);
        if(!StringUtils.hasLength(userName)||!StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo u = new UserInfo();
        u=userservice.getUserInfoByName(userName);
        if(u==null) {
            return false;
        }
        if(u.getPassword().equals(password)) {

           session.setAttribute(Constants.SESSION_USER_KEY,u);
            return true;
        }
       return false;
    }
}
